package com.example.gzysmartcity.ui.fragment.factory;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentFactoryBinding;
import com.example.gzysmartcity.repository.model.MyLine;
import com.example.gzysmartcity.repository.model.SupplyNum;
import com.example.gzysmartcity.ui.adapter.BrAdapter;
import com.example.gzysmartcity.ui.adapter.FactoryAdpater;
import com.example.gzysmartcity.ui.base.BaseFragment;
import com.example.gzysmartcity.viewmodel.FactoryViewModel;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnPageChangeListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class FactoryFragment extends BaseFragment<FragmentFactoryBinding> {
    @Override
    protected FragmentFactoryBinding viewBinding() {
        return FragmentFactoryBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        b.tvCarbuy.setOnClickListener(v->{jump(CarBuyFragment.class,null,null);});
        b.back.setOnClickListener(v->{remove();});
        FactoryViewModel factoryViewModel=new ViewModelProvider(this).get(FactoryViewModel.class);
        factoryViewModel.getSupplyData();
        factoryViewModel.getFactoryLiveData().observe(getViewLifecycleOwner(),state -> {
            switch (state){
                case SUCCESS:
                    List<SupplyNum> supplyNums=factoryViewModel.getSupplyNums();
                    FactoryAdpater adpater=new FactoryAdpater(R.layout.rv_factory);
                    adpater.setNewInstance(supplyNums);
                    b.rvFactory.setAdapter(adpater);
                    b.rvFactory.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL,false));
//                    b.rvFactory.setLayoutManager(new GridLayoutManager(getContext(),4));
                    b.tvFactoryAll.setText(factoryViewModel.getSupplyNumAll()+"");
                    adpater.addChildClickViewIds(R.id.rv_factory_cardView);
                    adpater.setOnItemClickListener((adaptera, view1, position) -> {
                        adpater.getPosotionColors(position);
                        adaptera.notifyDataSetChanged();
                    });
                    break;
                case FAIL:
                    break;
            }
        });
        factoryViewModel.getLineData();
        factoryViewModel.getFactoryLiveDat1().observe(getViewLifecycleOwner(),state -> {
            switch (state){
                case FAIL:
                    break;
                case SUCCESS:
                    List<MyLine> myLinesList=factoryViewModel.getMyLineList();
                    setDataFactory(myLinesList.get(0));
//                    List<Object> imageB=factoryViewModel.getBannerImageFactory(myLinesList);
                    b.bannerFactory.setAdapter(new BrAdapter(factoryViewModel.getBannerImageFactory(myLinesList)))
                            .addBannerLifecycleObserver(this)
                            .setLoopTime(5000)
                            .setIndicator(new CircleIndicator(getContext()),false);
                    b.bannerFactory.addOnPageChangeListener(new OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                        }

                        @Override
                        public void onPageSelected(int position) {
                            MyLine myLine=myLinesList.get(position);
                            setDataFactory(myLine);
                        }

                        @Override
                        public void onPageScrollStateChanged(int state) {

                        }
                    });
                    break;
            }
        });

    }

    public void setDataFactory(MyLine myLine){
        b.tvFhtime.setText(myLine.getTimeee());
        b.tvFBuzou.setText(myLine.getBuZou());
        b.tvFlinehuanjName.setText(myLine.getLinkNames());
        b.tvFlineName.setText(myLine.getLineNames());
        b.tvFNaijiu.setText(myLine.getNaiJiu());
        b.tvFscXiaoHao.setText(myLine.getXiaoHao());
        b.tvGongXuJieShao.setText(myLine.getProcessContent());
        b.tvGongXuName.setText(myLine.getProcessNames());
    }

    @Override
    protected String setTitle() {
        return null;
    }

    @Override
    protected boolean showBack() {
        return false;
    }

    @Override
    protected Boolean showBottom() {
        return null;
    }
}
